<template>
  <div>
    <!--搜索框-->
    <el-form :inline="true" :model="searchForm">
      <el-form-item>
        <el-input v-model="searchForm.keyStr"
                  placeholder="名称"
                  clearable>
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getGradeList">搜索</el-button>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="dialogFormVisible = true">新增</el-button>
      </el-form-item>

      <el-form-item>
        <el-popconfirm title="确定要批量删除吗？" @confirm="delHandle(null)">
          <el-button type="danger" slot="reference" :disabled="delBtlStatu">批量删除</el-button>
        </el-popconfirm>
      </el-form-item>
    </el-form>

    <!--多选Tab-->
    <el-table
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        stripe
        @selection-change="handleSelectionChange">
      <el-table-column
          type="selection"
          width="55">
      </el-table-column>

      <el-table-column
          prop="year"
          label="年份"
          width="120">
      </el-table-column>
      <el-table-column
          prop="name"
          label="年级名称"
          show-overflow-tooltip>
      </el-table-column>
      <el-table-column
          label="操作"
          width="250"
      >
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" @click="editHandle(scope.row)">编辑</el-button>
          <el-divider direction="vertical"></el-divider>
          <el-popconfirm title="确定要删除吗？" @confirm="delHandle(scope.row.id)">
            <el-button type="danger" icon="el-icon-delete" slot="reference">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <!--分页-->
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes, prev, pager, next, jumper"
        :page-sizes="[10, 20, 50, 100]"
        :current-page="current"
        :page-size="size"
        :total="total">
    </el-pagination>

    <!--新增对话框-->
    <el-dialog title="年级信息" :visible.sync="dialogFormVisible" width="600px" @closed="resetForm('editForm')">
      <el-form :model="editForm" :rules="editFormRules" ref="editForm">

        <el-form-item label="年份" prop="year" label-width="100px">
          <el-input v-model="editForm.year" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="年级名称" prop="name" label-width="100px">
          <el-input v-model="editForm.name" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('editForm')">取 消</el-button>
        <el-button type="primary" @click="submitEditForm('editForm')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "Grade",
  data() {
    return {
      searchForm: {},
      delBtlStatu: true,
      dialogFormVisible: false,
      total: 0,
      size: 10,
      current: 1,
      editForm: {},
      tableData: [],
      editFormRules: {
        name: [
          {required: true, message: '请输入年级名称', trigger: 'blur'}
        ],
        year: [
          {required: true, message: '请输入年份', trigger: 'blur'},
          {min: 4, max: 4, message: '4个数字表示年份', trigger: 'blur'}
        ]
      },
      multipleSelection: [],
      permDialogVisible: false,
      permForm: {},
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      permTreeData: []
    }
  },
  created() {
    this.getGradeList()
  },
  methods: {
    handleSelectionChange(val) {
      this.multipleSelection = val;
      this.delBtlStatu = val.length === 0
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.size = val
      this.getGradeList()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.current = val
      this.getGradeList()
    },
    resetForm(formName) {
      this.editForm = {
        id: null
      }
      this.dialogFormVisible = false
    },
    handleClose() {
      this.resetForm('editForm')
    },
    getGradeList() {
      this.$axios.get("/grade", {
        params: {
          keyStr: this.searchForm.keyStr,
          current: this.current,
          size: this.size
        }
      }).then(res => {
        // console.log("搜索角色列表：" + this.searchForm.name)
        console.log(res.data.data)
        this.tableData = res.data.data.records
        this.size = res.data.data.size
        this.current = res.data.data.current
        this.total = res.data.data.total
      })
    },
    submitEditForm(formName) {
      console.log(this.editForm)
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$axios.post('/grade', this.editForm)
              .then(res => {
                this.getGradeList()
                console.log(res.data)
                this.resetForm(formName)
                this.$message({
                  showClose: true,
                  message: '恭喜你，操作成功',
                  type: 'success'
                });
              })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    editHandle(data) {
      this.editForm = data
      this.dialogFormVisible = true
    },
    delHandle(id) {
      var ids = []
      if (id) {
        ids.push(id)
      } else {
        // 批量删除
        this.multipleSelection.forEach(row => {
          ids.push(row.id)
        })
      }
      this.$axios.delete("/grade", {
        data: ids
      }).then(res => {
        console.log(res)
        this.getGradeList()
        this.$message({
          showClose: true,
          message: '恭喜你，操作成功',
          type: 'success',
        });
      })
    },
  }
}
</script>

<style scoped>
.el-pagination {
  float: right;
  margin-top: 10px;
}
</style>